<?php

require_once("../../connection.php");

require_once('../../autor/dao/autor_dao.php');
require_once('../../autor/to/autor_to.php');


class Mysql_autor_dao implements Autor_dao{

	public function create($autor_to) {
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($autor_to->get_login());
			$password = mysql_real_escape_string($autor_to->get_password());
			$email = mysql_real_escape_string($autor_to->get_email());
			
			$result = mysql_query("insert into autor (login,password,email)
									values ('$login','$password','$email')",
									$link);			
			
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_INSERTED);
			
			Connection::singleton()->transaction_commit($link);
			
			return $autor_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function exist($login) {

		try {
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor 
									where login like '$login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function find_by_login_autor($login) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor 
									where login like '$login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) 
				throw new Exception(NOT_ROW_FOUND);

			Connection::singleton()->transaction_commit($link);
			
			return mysql_fetch_assoc($result);
		}
		catch (Exception $e) {			
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);		
		}
	}
	
	
	public function update($autor_to) {
	
		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($autor_to->get_login());
			$password = mysql_real_escape_string($autor_to->get_password());
			$email = mysql_real_escape_string($autor_to->get_email());
			
			$result = mysql_query("update autor 
									set password='$password', email='$email' 
									where login like '$login'", $link);
							
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_UPDATED);
			
			Connection::singleton()->transaction_commit($link);
			
			return $autor_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}		
	}
	
	
	public function remove($login) {	
		try {			
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("delete from autor
									where login like '$login'", $link);
									
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_DELETED);
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
}
?>